package com.epfresh.supplier.activity;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.util.Log;
import android.util.Pair;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.epfresh.api.entity.RequestEntityMapSupply;
import com.epfresh.api.entity.ResponseEntity;
import com.epfresh.api.global.BaseActivity;
import com.epfresh.api.global.BaseApplication;
import com.epfresh.api.global.BuyerConstant;
import com.epfresh.api.helper.ApplicationHelper;
import com.epfresh.api.http.OnRequestListener;
import com.epfresh.api.utils.CommonUtils;
import com.epfresh.api.utils.FormatUtil;
import com.epfresh.api.utils.JsonUtils;
import com.epfresh.api.utils.L;
import com.epfresh.api.utils.LocalDisplay;
import com.epfresh.api.widget.ShareDialog;
import com.epfresh.api.widget.SlideDetailsLayout;
import com.epfresh.api.widget.gradient.inter.GradientStyle;
import com.epfresh.api.widget.gradient.utils.GradientUtils;
import com.epfresh.api.widget.gradient.view.GradientActionBar;
import com.epfresh.api.widget.gradient.view.GradientScrollView;
import com.epfresh.supplier.R;
import com.epfresh.supplier.constant.Constant;
import com.epfresh.supplier.entity.AgencyListItem;
import com.epfresh.supplier.entity.BaseProduct;
import com.epfresh.supplier.entity.BaseProductAreaConfig;
import com.epfresh.supplier.entity.BaseProductAreaConfigPrices;
import com.epfresh.supplier.entity.BaseProductImages;
import com.epfresh.supplier.entity.productSpecEntity;
import com.epfresh.supplier.view.tag.FlowLayout;
import com.epfresh.supplier.view.tag.TagAdapter;
import com.epfresh.supplier.view.tag.TagFlowLayout;
import com.epfresh.supplier.widget.banner.BannerEntityImp;
import com.epfresh.supplier.widget.banner.OnBannerItemClickListener;
import com.epfresh.supplier.widget.banner.SliderBanner;
import com.epfresh.supplier.widget.banner.SliderBannerController;

import java.util.ArrayList;
import java.util.List;

public class BaseProductDetailActivity extends BaseActivity {

    private String mBaseStoreId;

    private String mProductId;

    private TextView tvBtnBuyer;

    private TextView mTvProductTitle;
    private TextView mTvAmountUpDown;
    private TextView mTvPostProduct;
    private TextView mTvProductIntroduction;
    private TextView mTvSendDate;
    private TextView mTvProductDescribe;

    private ImageView mVExpendArrow;

    private TagFlowLayout mTflBuyerPrice;
    private TagFlowLayout mTflPack;

    List<Pair<String, String>> mPackList = new ArrayList<>();
    List<BaseProductAreaConfigPrices> mPriceList = new ArrayList<>();
    List<BaseProductAreaConfigPrices> mPriceListAll = new ArrayList<>();
    TagAdapter<Pair<String, String>> mPackAdapter;
    TagAdapter<BaseProductAreaConfigPrices> mPriceAdapter;
    SliderBannerController mSliderBannerController;

    GradientActionBar mGradientToolbar;
    View mToolbarLine;
    GradientScrollView mGradientSv;
    ImageView mIvBack;
    ImageView mIvShare;
    private TextView mTvTitle;
    int top = -1;
    private TextView pull_flag;
    private TextView pull_flag_down;
    private SlideDetailsLayout container;
    private WebView webView;
    List<String> imagesList;

    private void initBanner() {

        mSliderBannerController = new SliderBannerController(this, (SliderBanner) findViewById(R.id.vp_banner));
        mSliderBannerController.setOnBannerItemClickListener(new OnBannerItemClickListener() {
            @Override
            public void onBannerItemClick(int position, BannerEntityImp entity) {

            }
        });


    }


    private void initView() {

        initBanner();
        container = (SlideDetailsLayout) findViewById(R.id.container);
        container.setOnSlideDetailsListener(new SlideDetailsLayout.OnSlideDetailsListener() {
            @Override
            public void onStatucChanged(SlideDetailsLayout.Status status, boolean isFirstOpen) {
                if (status == SlideDetailsLayout.Status.OPEN) {
                    if (isFirstOpen) {
                    }
                    loadDetails();
                    mTvTitle.setText("图文详情");
                    pull_flag.setVisibility(View.INVISIBLE);
                    pull_flag_down.setVisibility(View.VISIBLE);

                    mGradientToolbar.setGrade(0);
                } else {
                    mTvTitle.setText("商品详情");
                    pull_flag_down.setVisibility(View.INVISIBLE);
                    pull_flag.setVisibility(View.VISIBLE);
                    mGradientSv.setGradeWithHigh(top);
                }
            }
        });
        webView = (WebView) findViewById(R.id.webview_detail);
        pull_flag = (TextView) findViewById(R.id.pull_flag);
        pull_flag.setVisibility(View.INVISIBLE);
        pull_flag_down = (TextView) findViewById(R.id.pull_flag_down);
        mGradientToolbar = (GradientActionBar) findViewById(R.id.actionbarLayoutId);
//        ViewGroup.LayoutParams lp = mGradientToolbar.getLayoutParams();
//        lp.height = getResources().getDimensionPixelSize(R.dimen.height_title);
//        mGradientToolbar.setLayoutParams(lp);


        mToolbarLine = findViewById(R.id.v_toolbar_line);
        mIvBack = (ImageView) findViewById(R.id.iv_back);
        mIvShare = (ImageView) findViewById(R.id.iv_share);
        mTvTitle = (TextView) findViewById(R.id.tv_title);
        mToolbarLine.setVisibility(View.INVISIBLE);

        mIvBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        mIvShare.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                share();
            }
        });
        mGradientSv = (GradientScrollView) findViewById(R.id.scrollview_home);
        mGradientSv.setOnScrollChangedListener(new GradientScrollView.onScrollChangedListener() {
            @Override
            public void onScrollChanged(int l, int t, int oldl, int oldt, int totalHeight) {
                top = t;
                t = t > 0 ? t : 0;
                if (totalHeight == 0) {
                    totalHeight = 1;
                }
                if (t > totalHeight) {//滑动顶部
                    mToolbarLine.setVisibility(View.VISIBLE);
                    mIvBack.setBackgroundDrawable(updateGradientDrawable(0x0));
                    mIvShare.setBackgroundDrawable(updateGradientDrawable(0x0));
                    mTvTitle.setTextColor(Color.parseColor("#434343"));
                } else {
                    mTvTitle.setTextColor(Color.parseColor("#ffffff"));
                    mToolbarLine.setVisibility(View.INVISIBLE);
                    mIvBack.setBackgroundDrawable(updateGradientDrawable((totalHeight - t) * 0x20 / totalHeight));
                    mIvShare.setBackgroundDrawable(updateGradientDrawable((totalHeight - t) * 0x20 / totalHeight));
                }

            }
        });
        new GradientUtils().setColorMode(GradientStyle.ASC, 0xfc, 0xfc, 0xfc)
                // 执行绑定
                .bind(mGradientToolbar, mGradientSv);
        mTvProductTitle = (TextView) findViewById(R.id.tv_product_title);
        mTvAmountUpDown = (TextView) findViewById(R.id.tv_amount_up_down);
        mTvPostProduct = (TextView) findViewById(R.id.tv_product_post);
        mTvProductIntroduction = (TextView) findViewById(R.id.tv_product_buyer_introduction);
        mTvProductDescribe = (TextView) findViewById(R.id.tv_product_describe);
        mTvSendDate = (TextView) findViewById(R.id.tv_send_date);

        mTflBuyerPrice = (TagFlowLayout) findViewById(R.id.tfl_buyer_price);
        mTflPack = (TagFlowLayout) findViewById(R.id.tfl_pack);

        mVExpendArrow = (ImageView) findViewById(R.id.iv_product_bottom_arrow);

        mVExpendArrow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mVExpendArrow.getVisibility() == View.VISIBLE) {
                    expendPrice();
                }
            }
        });

        mTvPostProduct.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                intoPostProduct();
            }
        });
        mTvProductIntroduction.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                intoIntroduction();
            }
        });
        findViewById(R.id.tv_base).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                intoBaseStore();
            }
        });
        findViewById(R.id.tv_base_phone).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                intoPhone();
            }
        });
        //拼货
        tvBtnBuyer = (TextView) findViewById(R.id.tv_btn_buyer);
        //updateBottomStatus(false);
        tvBtnBuyer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                intoBuyer();
            }
        });

        reqBaseStoreProduct();

    }

    void updateBottomStatus(boolean isEnable){
        findViewById(R.id.ly_bottom).setVisibility(View.VISIBLE);
        if(isEnable){
            tvBtnBuyer.setText("拼货");
            tvBtnBuyer.setBackgroundColor(0xffff0000);
        }else {
            tvBtnBuyer.setText("暂无代办，不能拼货");
            tvBtnBuyer.setBackgroundColor(0xff959595);
        }

        tvBtnBuyer.setEnabled(isEnable);
    }


    private void intoBuyer() {

        reqAgency(null, mProductId);
    }


    private void intoPhone() {
        if (mBaseProduct != null) {
            String phone = mBaseProduct.getManagerPhone();
            if (phone != null && !"".equals(phone))
                CommonUtils.intoPhone(this, phone);
        }
    }

    private void intoIntroduction() {

        Intent intent = new Intent(this,WebActivity.class);

        intent.putExtra(Constant.KEY_WEB_URL, Constant.URL_HELP_BUYER_HOME);
        intent.putExtra(Constant.KEY_WEB_TITLE, "拼货说明");
        startActivity(intent);
    }

    private void intoBaseStore() {
        if (mBaseStoreId != null) {

            Intent intent = new Intent(this, BaseStoreInfoActivity.class);
            intent.putExtra("baseStoreId", mBaseStoreId);
            intent.putExtra("mHasAgent", getIntent().getBooleanExtra("mHasAgent",true));

            startActivity(intent);
        }
    }

    private void intoGrouponInfoPage(String grouponId, String productId, AgencyListItem item) {


        Intent intent = new Intent(this, GrouponInfoActivity.class);

        intent.putExtra("storeId", mBaseStoreId);
        intent.putExtra("grouponId", grouponId);
        intent.putExtra("productId", productId);
        intent.putExtra("isSingle", true);
        intent.putExtra("agencyItem", JsonUtils.toJson(item));
        startActivity(intent);
    }

    private void intoAgencyPage(String grouponId, String productId) {


        Intent intent = new Intent(this, AgencyListActivity.class);
        intent.putExtra("storeId", mBaseStoreId);
        intent.putExtra("grouponId", grouponId);
        intent.putExtra("productId", productId);
        startActivity(intent);
    }


    //发布到我的店铺
    private void intoPostProduct() {
        if (mBaseProduct != null && false) {
            Intent intent = new Intent(this, ProductAddActivity.class);
            intent.putExtra("is_first", false);
            intent.putExtra("productId", mBaseProduct.getId());
            intent.putExtra("listType", 3);

            intent.putExtra(Constant.TYPE, "1");
            startActivityForResult(intent, 0x23);
        }
    }

    private void loadDetails() {
        if (imagesList == null || imagesList.size() <= 0) {
            pull_flag.setVisibility(View.INVISIBLE);
        } else {
            webViewSetting();
            webView.setWebViewClient(new WebViewClient() {
                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    //返回值是true的时候控制去WebView打开，为false调用系统浏览器或第三方浏览器
//                    view.loadUrl(url);
                    return true;
                }
            });

            webView.loadDataWithBaseURL(null, getHtmlImg(), "text/html", "UTF-8", null);

        }
    }

    private void expendPrice() {

        if (mPriceList.size() > 3) {
            updatePriceList(false);
        } else {
            updatePriceList(true);
        }

        mPriceAdapter.notifyDataChanged();

    }

    BaseProduct mBaseProduct;

    public void updateDataView(BaseProduct baseProduct) {

        mBaseProduct = baseProduct;
        if (baseProduct != null) {

            mTvProductTitle.setText(baseProduct.getTitle());
            mTvProductDescribe.setText(baseProduct.getDescription());
            BaseProductAreaConfig areaConfig = baseProduct.getAreaConfig();
            String chargeUnit = baseProduct.getChargeUnit();


            if (mPriceAdapter == null) {
                mPriceAdapter = new TagAdapter<BaseProductAreaConfigPrices>(mPriceList) {
                    @Override
                    public View getView(FlowLayout parent, int position, BaseProductAreaConfigPrices baseProductAreaConfigPrices) {
                        View v = LayoutInflater.from(BaseProductDetailActivity.this).inflate(R.layout.tfl_item_product_price,
                                parent, false);
                        ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) v.getLayoutParams();
                        lp.width = (LocalDisplay.SCREEN_WIDTH_PIXELS - LocalDisplay.dp2px(24)) / 3;

                        TextView tvPrice = v.findViewById(R.id.tv_price);
                        TextView tvUnit = v.findViewById(R.id.tv_unit);
                        View vLine = v.findViewById(R.id.v_line);
                        String p = FormatUtil.subZeroAndDot(baseProductAreaConfigPrices.getPrice()) + "元/" + baseProductAreaConfigPrices.getChargeUnit();
                        tvPrice.setText(p);
                        tvUnit.setText(baseProductAreaConfigPrices.getOrderQuantityRange());
                        if (position == mPriceList.size() - 1 || position % 3 == 2) {
                            vLine.setVisibility(View.GONE);
                        } else {
                            vLine.setVisibility(View.VISIBLE);
                        }

                        return v;
                    }
                };
                mTflBuyerPrice.setAdapter(mPriceAdapter);
            }
            if (mPackAdapter == null) {
                mPackAdapter = new TagAdapter<Pair<String, String>>(mPackList) {
                    @Override
                    public View getView(FlowLayout parent, int position, Pair<String, String> post) {
                        View v = LayoutInflater.from(BaseProductDetailActivity.this).inflate(R.layout.tfl_item_product_pack,
                                parent, false);
                        ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) v.getLayoutParams();
                        lp.width = (LocalDisplay.SCREEN_WIDTH_PIXELS - LocalDisplay.dp2px(24)) / 2;

                        TextView tvSpeciesTitle = v.findViewById(R.id.tv_species_title);
                        TextView tvSpecies = v.findViewById(R.id.tv_species);
                        tvSpeciesTitle.setText(post.first);
                        tvSpecies.setText(post.second);


                        return v;
                    }
                };
                mTflPack.setAdapter(mPackAdapter);
            }


            mPriceList.clear();
            mPackList.clear();
            mPriceListAll.clear();
            if (areaConfig != null) {
                double maxOrderQuantity = areaConfig.getMaxOrderQuantity();
                double minOrderQuantity = areaConfig.getMinOrderQuantity();
                int minEda = areaConfig.getMinEda();
                int maxEda = areaConfig.getMaxEda();
                String date;
                if (minEda == maxEda) {
                    date = "预计配送时长：" + minEda + "天";
                } else {
                    date = "预计配送时长：" + minEda + "天~" + maxEda + "天";
                }
                mTvSendDate.setText(date);
                List<BaseProductAreaConfigPrices> prices = areaConfig.getPrices();
                if (prices != null) {
                    for (int i = 0; i < prices.size(); i++) {
                        BaseProductAreaConfigPrices b = prices.get(i);
                        b.setChargeUnit(chargeUnit);
                        mPriceListAll.add(b);
                    }
                }

                updatePriceList(false);
                //mTvAmountUpDown.setText(minOrderQuantity + chargeUnit + "起发货/" + maxOrderQuantity + chargeUnit + "上限");
                mTvAmountUpDown.setText("满" + minOrderQuantity + chargeUnit + "发货，可拼" + maxOrderQuantity + chargeUnit);
            }
            String s = baseProduct.getCategoryiiiName();
            if (s != null && !"".equals(s)) {
                mPackList.add(new Pair<>("品类：", s));
            }
            s = baseProduct.getOrigin();
            if (s != null && !"".equals(s)) {
                mPackList.add(new Pair<>("产地：", s));
            }
            s = baseProduct.getCategoryivName();
            if (s != null && !"".equals(s)) {
                mPackList.add(new Pair<>("品种：", s));
            }
             s = baseProduct.getSpecies();
            if (s != null && !"".equals(s)) {
                mPackList.add(new Pair<>("品名：", s));
            }
            s = baseProduct.getPack();
            if (s != null && !"".equals(s)) {
                mPackList.add(new Pair<>("包装：", s));
            }

            s = baseProduct.getLevel();
            if (s != null && !"".equals(s)) {
                mPackList.add(new Pair<>("等级：", s));
            }
            List<productSpecEntity> productSpec = baseProduct.getProductSpec();
            if (productSpec!=null&&productSpec.size()>0){
                for (int i=0;i<productSpec.size();i++){
                    productSpecEntity specEntity = productSpec.get(i);
                    mPackList.add(new Pair<>(specEntity.getSpecName()+"：", specEntity.getSpecOptionName()));
                }
            }

            if (mPriceListAll.size() < 4) {
                mVExpendArrow.setVisibility(View.INVISIBLE);
            } else {
                mVExpendArrow.setVisibility(View.VISIBLE);
            }
            mPriceAdapter.notifyDataChanged();
            mPackAdapter.notifyDataChanged();


            List<BaseProductImages> images = baseProduct.getImagesBanner();
            if (images != null && images.size() > 0) {
                mSliderBannerController.play(images);
            }

            imagesList = baseProduct.getImagesDetail();
            if (imagesList == null || imagesList.size() == 0) {
                pull_flag.setVisibility(View.VISIBLE);
                pull_flag.setBackgroundColor(Color.WHITE);
                pull_flag.setText("");
                pull_flag.setHint("");
                container.setIsOnlyFront(true);
            }

            if (false) {
                mTvPostProduct.setVisibility(View.VISIBLE);
            } else {
                mTvPostProduct.setVisibility(View.INVISIBLE);

                findViewById(R.id.v_line_product_post).setVisibility(View.INVISIBLE);
            }

            updateBottomStatus(mBaseProduct.isCanGroupon());
        }
    }

    private void updatePriceList(boolean isExpend) {
        mPriceList.clear();
        int size = mPriceListAll.size();
        if (!isExpend) {
            mVExpendArrow.setImageResource(R.mipmap.icon_base_product_up_arrow);
            size = size > 3 ? 3 : size;
        } else {
            mVExpendArrow.setImageResource(R.mipmap.icon_base_product_down_arrow);
        }
        for (int i = 0; i < size; i++) {
            mPriceList.add(mPriceListAll.get(i));
        }
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (savedInstanceState != null) {
            getIntent().putExtra("baseStoreId", savedInstanceState.getString("baseStoreId"));
            getIntent().putExtra("productId", savedInstanceState.getString("productId"));
        }
        mBaseStoreId = getIntent().getStringExtra("baseStoreId");
        mProductId = getIntent().getStringExtra("productId");
        setContentView(R.layout.activity_base_product_detail);

        initView();

    }

    @Override
    public void onSaveInstanceState(Bundle outState) {

        outState.putString("baseStoreId", mBaseStoreId);
        outState.putString("productId", mProductId);
        super.onSaveInstanceState(outState);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);


    }

    @Override
    protected void onRestart() {
        super.onRestart();

    }

    @Override
    protected void onResume() {
        super.onResume();


    }

    @Override
    protected void onPause() {
        super.onPause();


    }

    @Override
    protected void onDestroy() {
        if (webView != null) {
            webView.destroy();
        }
        super.onDestroy();
    }


    private void webViewSetting() {
        WebSettings ws = webView.getSettings();
//		ws.setUseWideViewPort(true);
        ws.setDefaultTextEncodingName("utf-8");
        ws.setLoadWithOverviewMode(true);
        ws.setJavaScriptEnabled(true);
        ws.setSupportZoom(true); // 设置可以支持缩放
        ws.setBuiltInZoomControls(false);
        ws.setLoadWithOverviewMode(true);
        ws.setPluginState(WebSettings.PluginState.ON);
        ws.setAllowFileAccess(true);
        ws.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        ws.setDomStorageEnabled(true);
        ws.setCacheMode(WebSettings.LOAD_DEFAULT);
        // DisplayMetrics metrics = new DisplayMetrics();
        // getWindowManager().getDefaultDisplay().getMetrics(metrics);

    }

    private String getHtmlImg() {
        StringBuilder sb = new StringBuilder();
        String htmlHead = "<html><head><style type=\"text/css\">body {margin:0px}</style><meta name=\"viewport\" content=\"width=device-width\"></head><body >";
        String htmlEnd = "</body></html>";
        sb.append(htmlHead);
        for (int i = 0; i < imagesList.size(); i++) {
            String url = imagesList.get(i);
            if (url != null & !"".equals(url)) {
                sb.append("<img src=\"");
                sb.append(url);
                sb.append("\" width = 100%/>");
            }
        }
        sb.append(htmlEnd);
        return sb.toString();
    }

    protected GradientDrawable updateGradientDrawable(int alpha) {
        GradientDrawable gd = new GradientDrawable();
        gd.setShape(GradientDrawable.OVAL);
        gd.setColor(Color.BLACK);
        gd.setAlpha(alpha);

        return gd;
    }

    @Override
    protected View getToolBar() {
        return null;
    }


    ShareDialog dialog;

    private void share() {
        if (mBaseProduct == null) {
            return;
        }
        if (dialog == null)
            dialog = new ShareDialog(this);

//        common/product_supply.html

//        URL_SHARE_BASE_PRODUCT_DETAIL
        String url = com.epfresh.api.constant.Constant.URL_SHARE_BASE_PRODUCT_DETAIL + mBaseProduct.getId() + "&" + BaseApplication.getInstance().getStoreId() ;
        List<BaseProductImages> images = mBaseProduct.getAttachments();
        String imgUrl = "http://m.epfresh.com/images/app-logo.png";
        if (images != null && images.size() > 0) {
            BaseProductImages baseProductImages = images.get(0);
            if (baseProductImages != null) {

                imgUrl = baseProductImages.getBannerUrl();
            }
        }
        String content = mBaseProduct.getDescription();
        if (content == null) {
            content = "国内领先的农产品交易服务平台，网上批发、产地直采，更省、更赚钱";
        }
        String title = mBaseProduct.getTitle();
        if (title == null) {
            title = "e批生鲜批发商APP";
        }
        dialog.showDialog(url, imgUrl, content, title);
    }


   private void reqBaseStoreProduct() {

        RequestEntityMapSupply RequestEntityMapSupply = new RequestEntityMapSupply();
//        RequestEntityMapSupply.setCmd("supplier/product/detail");
        RequestEntityMapSupply.setCmd(BuyerConstant.CMD_BUYER_PRODUCT_DETAIL);
        RequestEntityMapSupply.putParameter("agentStoreId", BaseApplication.getInstance().getStoreId());
        RequestEntityMapSupply.putParameter("productId", mProductId);
        RequestEntityMapSupply.putParameter("baseStoreId", mBaseStoreId);
        requestBuyer(RequestEntityMapSupply, BuyerConstant.CMD_BUYER_PRODUCT_DETAIL, new OnRequestListener<BaseProduct>() {
            @Override
            public BaseProduct jsonToObj(String responseStr) {
                return JsonUtils.fromJson(responseStr, BaseProduct.class);
            }

            @Override
            public void onFail(int failCode, Object arg, Object tag) {

            }

            @Override
            public void onResponseError(int failCode, Object arg, Object tag) {

            }

            @Override
            public void onStart(Object tag) {

            }

            @Override
            public void onResponse(ResponseEntity<BaseProduct> response, Object tag) {
                updateDataView(response.getResponseElement());
            }
        });
    }

    boolean isLockAgency;

    private void reqAgency(final String grouponId, final String productId) {
        if (isLockAgency) {
            return;
        }
        isLockAgency = true;
        RequestEntityMapSupply data = new RequestEntityMapSupply();
        data.setCmd(BuyerConstant.CMD_BUYER_PRODUCT_AGENT_STORES);
        data.setClientIdentifierCode(getAppApplication().getClientIdentifierCode());
        data.setStoreId(BaseApplication.getInstance().getStoreId());

        data.putParameter("agentStoreId", BaseApplication.getInstance().getStoreId());
        data.putParameter("grouponId", grouponId);
        data.putParameter("productId", productId);


        requestBuyer(data, BuyerConstant.CMD_BUYER_PRODUCT_AGENT_STORES, new OnRequestListener<List<AgencyListItem>>() {
            @Override
            public List<AgencyListItem> jsonToObj(String responseStr) {
                return JsonUtils.fromJsonList(responseStr, AgencyListItem.class);
            }

            @Override
            public void onFail(int failCode, Object arg, Object tag) {
                isLockAgency = false;
                hideProgressDialog();
            }

            @Override
            public void onResponseError(int failCode, Object arg, Object tag) {
                hideProgressDialog();
                isLockAgency = false;
            }

            @Override
            public void onStart(Object tag) {

                showProgressDialog();
            }

            @Override
            public void onResponse(ResponseEntity<List<AgencyListItem>> response, Object tag) {
                List<AgencyListItem> responseElement = response.getResponseElement();
                hideProgressDialog();
                isLockAgency = false;
                if (responseElement != null) {

                    if (responseElement.size() == 1) {
                        AgencyListItem item = responseElement.get(0);

                        intoGrouponInfoPage(grouponId, productId, item);
                    } else if (responseElement.size() == 0) {
                        Toast.makeText(mContext, "该商品未找到代办店铺信息，请刷新后重试！", Toast.LENGTH_SHORT).show();
                    } else {
                        intoAgencyPage(grouponId, productId);
                    }
                } else {
                    Toast.makeText(mContext, "该商品未找到代办店铺信息，请刷新后重试！", Toast.LENGTH_SHORT).show();
                } 

            }
        });
    }


}
